﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using HZ.Supervise.Model.Enum;
using HZ.Supervise.DAL;

namespace HZ.Supervise.Web.Secondphase
{
	public partial class ProcureCompanyPS : BasePage
	{

		HZ.Supervise.IBLL.IBLL_User ibllEntity = BLLFactory.BLL_User();

        public ProcureCompanyPS()
        {
            this.ModuleNo = Module.趋势走向.ToString();
            this.PageOperateType = OperateType.配送排名.ToString();
        }
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				this.AreaControls1.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;
				MaxforMonth1.Banding();
				Databing();
			}
			//Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>Load()</script>");
		}

		private void Databing()//modify by 曹鹏 2011年5月18日16:55:26 企业排名数据绑定
		{
            try
            {

			StringBuilder wherePm = new StringBuilder();

			wherePm = HZ.Supervise.Web.Function.GetConditionOfAreaId(wherePm, this.AreaControls1.SelectedAreaID) as StringBuilder;

			wherePm.AppendFormat("   AND daytime='{0}'", this.MaxforMonth1.Getvalue());

			StringBuilder wherePms = new StringBuilder();

			wherePms = HZ.Supervise.Web.Function.GetConditionOfAreaId(wherePms, this.AreaControls1.SelectedAreaID) as StringBuilder;

			string _oldYearmonth = this.MaxforMonth1.Getvalue().Insert(4, "-") + "-01";//根据本月拼接字符串，以便于能够转化成datetime类型，从而能够调用datetime类中求得上个月的方法

			wherePms.AppendFormat("  AND daytime='{0}'", Convert.ToDateTime(_oldYearmonth).AddMonths(-1).ToString("yyyyMM"));
            //修改者：蒋峰（2011-060-23）
            //备  注：现在用不上 
			string sortSource = "''";

            if (this.dropMort.SelectedValue == "0" || this.dropMort.SelectedValue == "1" || this.dropMort.SelectedValue == "2")
            {

                wherePm.AppendFormat(" and SortSorce = {0}", this.dropMort.SelectedValue);

                wherePms.AppendFormat(" and SortSorce = {0}", this.dropMort.SelectedValue);

                sortSource = this.dropMort.SelectedValue;

            }

			string orderOfrank = "";

			string orderBy = "";

			if (this.ddlorderby.SelectedValue == "PurchaseAmount" || this.ddlorderby.SelectedValue == "DistributeAmount" || this.ddlorderby.SelectedValue == "WareHouseAmount")
			{

				orderOfrank = " sum(" + this.ddlorderby.SelectedValue + ")";

				orderBy = this.ddlorderby.SelectedValue + "total  " + this.dllorder.SelectedValue;

			}
			else
			{

				if (this.ddlorderby.SelectedValue == "WareHouseRate")
				{
                    orderOfrank = " (case when SUM(WarehouseAmount) = 0 then 0 when SUM(PurchaseAmount)=0 then 0 when CONVERT(decimal(18,3),CONVERT(float, SUM(WarehouseAmount)) / SUM(PurchaseAmount))>1 then 1 else  CONVERT(decimal(18,3),CONVERT(float, SUM(WarehouseAmount)) / SUM(PurchaseAmount)) end)";
				}
				else
				{

                    orderOfrank = " (case when SUM(DistributeAmount) = 0 then 0 when SUM(PurchaseAmount)=0 then 0 when CONVERT(decimal(18,3),CONVERT(float, SUM(DistributeAmount)) / SUM(PurchaseAmount))>1 then 1 else CONVERT(decimal(18,3),CONVERT(float, SUM(DistributeAmount)) / SUM(PurchaseAmount)) end)";

				}

				orderBy = this.ddlorderby.SelectedValue + " " + this.dllorder.SelectedValue;

			}

			int totalrecord = 0;

			string whereCompanyPs = "";
			 
			if (!string.IsNullOrEmpty(this.txtCompanynamePs.Text))
			{

				whereCompanyPs = "  and A.CompanyName like '%" + txtCompanynamePs.Text.Trim() + "%'";

			}

			HZ.Supervise.IBLL.IBll_CompanyOrderDetail entity = BLLFactory.IBll_CompanyOrderDetail();

			DataTable dt = entity.GeteCompany_PSRanking1(orderOfrank, orderBy, wherePm.ToString(), wherePms.ToString(), whereCompanyPs, sortSource, AspNetPager1.CurrentPageIndex,AspNetPager1.PageSize, out totalrecord);

			gvwProcureCatalog.Visible = true;

			gvwProcureCatalog2.Visible = false;

			Function.ChangeRate(dt, "WarehouseRate", "addWarehouseRate");

			Function.ChangeRate(dt, "DistributeRate", "addDistributeRate");

			Function.AddColumn(dt, "SortSource");

			Function.AddColRank(dt, "pms");

			gvwProcureCatalog.DataSource = dt;

			gvwProcureCatalog.DataBind();

			this.AspNetPager1.RecordCount = totalrecord;

            }
            catch (Exception e)
            {
                e.ToString();

                HZ.Web.WebUtil.Alert("请输入正确的查询格式！");
            }
		}

		protected void AspNetPager1_PageChanged(object sender, EventArgs e)
		{
			Databing();
		}

		protected void btnQuery_Click(object sender, EventArgs e)
		{
			this.AspNetPager1.CurrentPageIndex = 1;
		}

		protected void btnExcel_Click(object sender, EventArgs e)
		{
	
			StringBuilder wherePm = new StringBuilder();

			wherePm = HZ.Supervise.Web.Function.GetConditionOfAreaId(wherePm, this.AreaControls1.SelectedAreaID) as StringBuilder;

			wherePm.AppendFormat("   AND daytime='{0}'", this.MaxforMonth1.Getvalue());

			StringBuilder wherePms = new StringBuilder();

			wherePms = HZ.Supervise.Web.Function.GetConditionOfAreaId(wherePms, this.AreaControls1.SelectedAreaID) as StringBuilder;

			string _oldYearmonth = this.MaxforMonth1.Getvalue().Insert(4, "-") + "-01";//根据本月拼接字符串，以便于能够转化成datetime类型，从而能够调用datetime类中求得上个月的方法

			wherePms.AppendFormat("  AND daytime='{0}'", Convert.ToDateTime(_oldYearmonth).AddMonths(-1).ToString("yyyyMM"));
            //修改者：蒋峰（2011-060-23）
            //备  注：现在用不上 
			string sortSource = "1";

            if (this.dropMort.SelectedValue == "0" || this.dropMort.SelectedValue == "1" || this.dropMort.SelectedValue == "2")
            {

                wherePm.AppendFormat(" and SortSorce = {0}", this.dropMort.SelectedValue);

                wherePms.AppendFormat(" and SortSorce = {0}", this.dropMort.SelectedValue);

                sortSource = this.dropMort.SelectedValue;

            }

			string orderOfrank = "";

			string orderBy = "";

			if (this.ddlorderby.SelectedValue == "PurchaseAmount" || this.ddlorderby.SelectedValue == "DistributeAmount" || this.ddlorderby.SelectedValue == "WareHouseAmount")
			{

				orderOfrank = " sum(" + this.ddlorderby.SelectedValue + ")";

				orderBy = this.ddlorderby.SelectedValue + "total  " + this.dllorder.SelectedValue;

			}
			else
			{

				if (this.ddlorderby.SelectedValue == "WareHouseRate")
				{
                    orderOfrank = " (case when SUM(WarehouseAmount) = 0 then 0 when SUM(PurchaseAmount)=0 then 0 when CONVERT(decimal(18,3),CONVERT(float, SUM(WarehouseAmount)) / SUM(PurchaseAmount))>1 then 1 else  CONVERT(decimal(18,3),CONVERT(float, SUM(WarehouseAmount)) / SUM(PurchaseAmount)) end)";
				}
				else
				{

                    orderOfrank = " (case when SUM(DistributeAmount) = 0 then 0 when SUM(PurchaseAmount)=0 then 0 when CONVERT(decimal(18,3),CONVERT(float, SUM(DistributeAmount)) / SUM(PurchaseAmount))>1 then 1 else CONVERT(decimal(18,3),CONVERT(float, SUM(DistributeAmount)) / SUM(PurchaseAmount)) end)";

				}

				orderBy = this.ddlorderby.SelectedValue + " " + this.dllorder.SelectedValue;

			}

			string whereCompanyPs = "";

			if (!string.IsNullOrEmpty(this.txtCompanynamePs.Text))
			{

				whereCompanyPs = "  and A.CompanyName like '%" + txtCompanynamePs.Text.Trim() + "%'";

			}

			int totalrecord = 0;

			HZ.Supervise.IBLL.IBll_CompanyOrderDetail entity = BLLFactory.IBll_CompanyOrderDetail();

			DataTable dt = entity.GeteCompany_PSRanking1(orderOfrank, orderBy, wherePm.ToString(), wherePms.ToString(), whereCompanyPs,sortSource, 1, 0, out totalrecord);

			gvwProcureCatalog.Visible = true;

			gvwProcureCatalog2.Visible = false;

			Function.ChangeRate(dt, "WarehouseRate", "addWarehouseRate");

			Function.ChangeRate(dt, "DistributeRate", "addDistributeRate");

			Function.AddColumn(dt, "SortSource");

			Function.AddColRank(dt, "pms");

			gvwProcureCatalog.DataSource = dt;

			gvwProcureCatalog.DataBind();

			this.AspNetPager1.RecordCount = totalrecord;

			ExportFile.Instance.ExportData(dt, gvwProcureCatalog, this);

		}
	}
}